<template>
	<view class="goodsInfo">
		<uni-search-bar placeholder="请输入商品名称进行查询" style="background-color: #fff;" @confirm="search"
			v-model="queryParam.NameorCode" @cancel="cancel">
		</uni-search-bar>
		<view class="container">
			<view class="left">
				<view :class="current==index?'leftItem active':'leftItem'" v-for="item,index in typeData" :key="item.Id"
					@click="changeType(index,item.Id)">
					{{item.Name}}
				</view>
			</view>
			<view class="right">
				<scroll-view style="height: calc(100% - 100rpx);
				box-sizing: border-box;
				padding-bottom: 30rpx;" scroll-y="true" @scrolltolower="lower">
					<uni-list>
						<uni-list-item v-for="item in data" :key="item.Id" :title="item.cInvName"
							:note="'￥'+momenyFormat(item.iInvRCost)"
							:thumb="item.Picture?item.Picture:'/static/none.png'" thumb-size="lg" clickable
							@click="toForm(item.Id)">
							<template v-slot:footer>
								<view @click.stop="()=>{}">
									<uni-icons @click="del(item.Id)" color="red" type="trash"></uni-icons>
								</view>
							</template>
						</uni-list-item>
					</uni-list>
				</scroll-view>
			</view>
		</view>
		<view class="tabbar">
			<view class="submitOrder" @click="submitOrderCart">
				去复制
			</view>
		</view>
	</view>
</template>

<script>
	import {
		momenyFormat
	} from '@/util/util.js'
	export default {
		data() {
			return {
				typeData: [],
				data: [],
				pagination: {
					PageIndex: 1,
					PageRows: 30,
					SortField: "CreateTime",
					SortType: "desc",
				},
				total: 0,
				queryParam: {
					"Condition": "Name",
					"Keyword": "",
					"CommpayId": "",
				},
				current: 0
			}
		},
		onNavigationBarButtonTap(e) {
			//console.log("”“”“”“”“"+JSON.stringify(this.typeData[this.current]));
			if (this.typeData.length <= 0) return uni.showToast({
				icon: 'none',
				title: '请先添加分类'
			})
			if (this.typeData[this.current].Id == 0) return uni.showToast({
				icon: 'none',
				title: '请选择具体分类'
			})			
			uni.navigateTo({
				url: '/pages/Information/page/GoodsInfo/GoodsInfoForm?pid=' + this.typeData[this.current].Id+'&name='+this.typeData[this.current].Name
			})
		},
		onLoad() {
			this.queryParam.CommpayId = uni.getStorageSync('CompanyInfo').Id
			this.getTypeList()
		},
		onShow() {
			if (this.typeData.length <= 0) return
			this.pagination.PageIndex = 1
			this.getDataList(this.typeData[this.current].Id)
		},
		methods: {
			del(Id) {
				uni.showModal({
					title: '提示',
					content: '确认删除此商品吗?',
					success: (val) => {
						if (val.confirm) {
							this.$http('post', '/BO/Inventory/DeleteData', [Id]).then(res => {
								if (res.Success) {
									this.$api.msg('操作成功')
									this.pagination.PageIndex = 1
									this.getDataList(this.typeData[this.current].Id)
								} else this.$api.msg(res.Msg)
							})
						}
					}
				})
			},
			// 去复制
			submitOrderCart() {
				uni.navigateTo({
					url: '/pages/Information/page/GoodsInfo/GoodsInfoCopy'
				})
			},
			lower() {
				if (this.data.length >= this.total) return
				console.log('123');
				this.pagination.PageIndex++
				uni.showLoading()
				this.$http('post', '/BO/BIlling/GetAppInventdata', {
						...this.pagination,
						Search: {
							TypeId: this.typeData[this.current].Id,
							...this.queryParam
						}
					})
					.then(res => {
						uni.hideLoading()
						if (res.Success) {
							this.data = this.data.concat(res.Data)
							this.total = res.Total
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
					})
			},
			// 查询
			search() {
				if (this.typeData.length <= 0) return uni.showToast({
					icon: 'none',
					title: '请先添加分类'
				})
				this.current = 0
				this.pagination.PageIndex = 1
				this.getDataList(this.typeData[this.current].Id)
			},
			// 左侧点击
			changeType(index, pid) {
				this.current = index
				this.pagination.PageIndex = 1
				this.queryParam.NameorCode = ''
				this.getDataList(pid)
			},
			// 分类列表
			getTypeList() {
				uni.showLoading()
				this.$http('post', '/BO/InventType/GetDataList', {
						Search: {
							CommpayId: uni.getStorageSync('CompanyInfo').Id
						}
					})
					.then(res => {
						uni.hideLoading()
						if (res.Success) {
							this.typeData = res.Data
							if (this.typeData.length > 0) {
								this.typeData.unshift({
									Name: '全部',
									Id: '0'
								})
								this.getDataList(this.typeData[this.current].Id)
							}
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
						console.log(this.total);
					})
			},
			// 商品列表
			getDataList(pid) {
				uni.showLoading()
				this.$http('post', '/BO/BIlling/GetAppInventdata', {
						...this.pagination,
						Search: {
							TypeId: pid,
							...this.queryParam
						}
					})
					.then(res => {
						uni.hideLoading()
						if (res.Success) {
							this.data = res.Data
							this.total = res.Total
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
					})
			},
			toForm(id) {
				uni.navigateTo({
					url: '/pages/Information/page/GoodsInfo/GoodsInfoForm?pid=' + this.typeData[this.current].Id +
						'&Id=' +
						id
				})
			},
			cancel() {
				this.queryParam.Keyword = ''
				if (this.typeData.length > 0) this.getDataList(this.typeData[this.current].Id)
			},
			momenyFormat(num) {
				return momenyFormat(num)
			}
		}
	}
</script>

<style scoped lang="scss">
	.goodsInfo {
		display: flex;
		flex-direction: column;
		height: 100%;

		.container {
			display: flex;
			background-color: #fff;
			// height: calc(100% - 56px - 120rpx);
			height: calc(100% - 120rpx);

			.left {
				width: 100px;
				background-color: #E8F4FF;
				text-align: center;
				overflow-y: scroll;
				height: calc(100% - 100rpx);
				box-sizing: border-box;
				padding-bottom: 30rpx;

				.leftItem {
					line-height: 2.5;
					font-weight: 600;
					color: #999;
					font-size: 15px;

					&:active {
						background-color: #fff;
					}
				}

				.active {
					color: #000;
					background-color: #fff;
				}
			}

			.right {
				flex: 1;
				// overflow-y: scroll;
				max-height: 100%;
			}
		}

		.tabbar {
			display: flex;
			width: 100%;
			height: 120rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			background-color: #ffffff;
			position: fixed;
			bottom: 0;
			border-top: 1px solid #E0E0E0;
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.submitOrder {
				width: 208rpx;
				height: 80rpx;
				background-image: linear-gradient(to right, #38FFCB, #2979FF);
				border-radius: 60rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #FFFFFF;
				letter-spacing: 2.57rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>